<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col>
      <strong v-if="isRoot">{{ nodeData.name }}</strong>
      <span v-else>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="6">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>
          {{ nodeData.manager }}
        </el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addFn">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="
                  !isRoot"
                @click.native="editFn"
              >编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartmentsFn">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    nodeData: {
      // 定义类型
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async delDepartmentsFn() {
      // Error in v-on handler (Promise/async):"??"此类报错都是promise引起的,需要try{}catch(err){}捕获错误
      try {
        await this.$confirm('确定删除?')
        // 删除该项
        await delDepartments(this.nodeData.id)
        // 提示用户删除成功
        this.$message.success('删除成功!')
        // 通知父组件已删除该项,更新页面
        this.$emit('delDepartments')
      } catch (error) {
        console.log(error)
      }
    },

    // 添加子部门事件
    addFn() {
      // 通知父组件新增
      this.$emit('addDepartments', this.nodeData)
    },

    // 编辑子部门事件
    editFn() {
      // 通知父组件编辑
      this.$emit('editDepartments', this.nodeData)
    }
  }
}
</script>

<style>

</style>
